﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using ExcellentCRM.DataService.Models;

namespace ExcellentCRM.DataService.Implements
{
    public class StuClientService : BaseService<StuClient>
    {
        public IList<StuClient> Search(StuClient stuClient, int pageSize, int pageIndex,
                                       out int totalCount)
        {
            using (var dbCtx = new DataServiceContext())
            {
                var searchQuery =
                    dbCtx.StuClients.Include("Consultant").Include("ContactRecords").Where(p => p.StatusToken != 0);
                if (stuClient.ConsultantId > 0)
                    searchQuery = searchQuery.Where(p => p.ConsultantId == stuClient.ConsultantId);
                if (!string.IsNullOrWhiteSpace(stuClient.StudentInfo.StuName))
                    searchQuery = searchQuery.Where(p => p.StudentInfo.StuName.Contains(stuClient.StudentInfo.StuName));
                if (!string.IsNullOrWhiteSpace(stuClient.StudentInfo.Phone))
                    searchQuery = searchQuery.Where(p => p.StudentInfo.Phone.Contains(stuClient.StudentInfo.Phone));
                if (!string.IsNullOrWhiteSpace(stuClient.StudentInfo.CellPhone))
                    searchQuery = searchQuery.Where(p => p.StudentInfo.CellPhone.Contains(stuClient.StudentInfo.CellPhone));
                if (!string.IsNullOrWhiteSpace(stuClient.StudentInfo.Email))
                    searchQuery = searchQuery.Where(p => p.StudentInfo.Email.Contains(stuClient.StudentInfo.Email));
                if (!string.IsNullOrWhiteSpace(stuClient.StudentInfo.Profession))
                    searchQuery = searchQuery.Where(p => p.StudentInfo.Profession.Contains(stuClient.StudentInfo.Profession));
                if (!string.IsNullOrWhiteSpace(stuClient.StudentInfo.CompanyName))
                    searchQuery = searchQuery.Where(p => p.StudentInfo.CompanyName.Contains(stuClient.StudentInfo.CompanyName));
                if (!string.IsNullOrWhiteSpace(stuClient.StudentInfo.Position))
                    searchQuery = searchQuery.Where(p => p.StudentInfo.Position.Contains(stuClient.StudentInfo.Position));

                totalCount = searchQuery.Count();

                var searchResult =
                    searchQuery.OrderByDescending(p => p.Id)
                               .Skip(pageSize*(pageIndex - 1))
                               .Take(pageSize)
                               .ToList();
                return searchResult;
            }
        }

        //todo:是否需要姓名+单位名称来排重?
        public bool StuClientIsExists(string stuName, string phone, string cellPhone, string email, long? id)
        {
            using (var dbCtx = new DataServiceContext())
            {
                if (string.IsNullOrWhiteSpace(phone) && string.IsNullOrWhiteSpace(cellPhone) &&
                    string.IsNullOrWhiteSpace(email))
                    return false;
                long idVal = id ?? -1;
                //创建时间小于6个月前的可以再次更新，可以更新培训顾问
                var preSixMonthDate = DateTime.Now.AddMonths(-6);
                var searchQuery =
                    dbCtx.StuClients.Where(p => p.StudentInfo.StuName == stuName && p.Id != idVal &&
                                                p.CreatedTime > preSixMonthDate);
                if (!string.IsNullOrWhiteSpace(phone))
                    searchQuery = searchQuery.Where(p => p.StudentInfo.Phone == phone);
                if (!string.IsNullOrWhiteSpace(cellPhone))
                    searchQuery = searchQuery.Where(p => p.StudentInfo.CellPhone == cellPhone);
                if (!string.IsNullOrWhiteSpace(email))
                    searchQuery = searchQuery.Where(p => p.StudentInfo.Email == email);

                return searchQuery.Any();
            }
        }

        public StuClient GetModel(string stuName, string phone, string cellPhone, string email)
        {
            using (var dbCtx = new DataServiceContext())
            {
                var searchQuery = dbCtx.StuClients.Where(p => p.StudentInfo.StuName == stuName);
                if (!string.IsNullOrWhiteSpace(phone))
                    searchQuery = searchQuery.Where(p => p.StudentInfo.Phone == phone);
                if (!string.IsNullOrWhiteSpace(cellPhone))
                    searchQuery = searchQuery.Where(p => p.StudentInfo.CellPhone == cellPhone);
                if (!string.IsNullOrWhiteSpace(email))
                    searchQuery = searchQuery.Where(p => p.StudentInfo.Email == email);

                return searchQuery.FirstOrDefault();
            }
        }

        public override ServiceResult Save(StuClient model)
        {
            using (var dbCtx = new DataServiceContext())
            {
                if (model.Id > 0)
                {
                    var originalModel = dbCtx.StuClients.FirstOrDefault(p => p.Id == model.Id);
                    dbCtx.Entry(originalModel).CurrentValues.SetValues(model);
                }
                else
                {
                    dbCtx.StuClients.Add(model);
                }
                dbCtx.SaveChanges();
                return ServiceResult.SuccessResult;
            }
        }

        public ContactRecord GetContactRecord(long contactId)
        {
            using (var dbCtx = new DataServiceContext())
            {
                return dbCtx.ContactRecords.FirstOrDefault(p => p.Id == contactId);
            }
        }

        public ServiceResult SaveContactRecord(ContactRecord contactRecord)
        {
            using (var dbCtx = new DataServiceContext())
            {
                if (contactRecord.Id > 0)
                {
                    dbCtx.ContactRecords.Attach(contactRecord);
                    dbCtx.Entry(contactRecord).State = EntityState.Modified;
                }
                else
                {
                    dbCtx.ContactRecords.Add(contactRecord);
                }
                dbCtx.SaveChanges();
                return ServiceResult.SuccessResult;
            }
        }
    }
}